﻿; 需要指定unicode，否则脚本中执行命令中汉字及弹窗汉字乱码
Unicode true

; 自定义变量
!define PRODUCT_CH_NAME "翼丰信"
!define INSTALL_DISPLAY_NAME "${PRODUCT_CH_NAME}${OLD_VERSION}-to-${NEW_APP_VERSION}增量更新"

!include MUI2.nsh
; 安装过程页面
!insertmacro MUI_PAGE_INSTFILES
; 安装完成页面是否启动checkbox
!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_TEXT "运行 ${PRODUCT_CH_NAME}"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
; 安装完成页面
!insertmacro MUI_PAGE_FINISH
; 因为本脚本需要保存为UTF8 BOM格式，但以下语言设置只支持UTF8格式，所以需要将原有的SimpChinese.nsh和nlf文件另存为bom编码格式并引用（如下）
; 安装界面包含的语言设置
!insertmacro MUI_LANGUAGE "SimpChineseCustomized"

!include FileFunc.nsh
!include LogicLib.nsh
!insertmacro GetParent

; 定义全局变量
Var /GLOBAL apppath
Var /GLOBAL restart

; Name "${PRODUCT_NAME}-Delta-Updater"
Name "${INSTALL_DISPLAY_NAME}"
OutFile "${INSTALLER_OUTPUT_PATH}"
; 需要管理员权限，否则安装及写入注册表会失败
RequestExecutionLevel admin
; 显示安装详情
ShowInstDetails show

; 属性中详细信息 语言：中文（简体，中国）
; 版本号，格式为 X.X.X.X (若使用则本条必须)
VIProductVersion "${NEW_APP_VERSION}.0"
; 文件说明
VIAddVersionKey /LANG=2052 "FileDescription" "${INSTALL_DISPLAY_NAME}"
; 文件版本
VIAddVersionKey /LANG=2052 "FileVersion" "${NEW_APP_VERSION}.0"
; 产品名称
VIAddVersionKey /LANG=2052 "ProductName" "${PRODUCT_CH_NAME}"
; 产品版本
VIAddVersionKey /LANG=2052 "ProductVersion" "${NEW_APP_VERSION}"
; 版权信息
VIAddVersionKey /LANG=2052 "LegalCopyright" "Copyright © ${CURRENT_YEAR} ${COMPANY_NAME}"

Icon "${PRODUCT_ICON_PATH}"
; 是否静默安装silent/normal
;SilentInstall normal

;读取注册表安装路径到$INSTDIR，最后的空为键值（写入时就没指定，所以注册表中为默认），如果取不到为默认路径
InstallDirRegKey HKCU "Software\com.yf-pc-chat-yfx.install-guid" ""

# avoid exit code 2
!macro quitSuccess
  SetErrorLevel 0
  Quit
!macroend

Section "gen_package" SEC01
  ; 判断安装路径是否为空
  Call CheckInstallPath

  ;!define NSIS_CONFIG_LOG "$INSTDIR\nsis.log"
  ;LogSet on

  ;获取 $INSTDIR 的上一级目录
  ${GetParent} $INSTDIR $R0
  ; $INSTDIR为增量更新的安装文件所在路径
  StrCpy $INSTDIR "$R0\${PRODUCT_NAME}-delta-updater"
  ; $apppath为原程序安装路径
  StrCpy $apppath "$R0\${PRODUCT_NAME}"
  ; DetailPrint "message2: apppath: $apppath"

  ${GetParameters} $0
  ${GetOptions} $0 "/APPPATH=" $apppath
  ${GetParameters} $0
  ${GetOptions} $0 "/RESTART=" $restart

  ; DetailPrint "message: args: $0"
  ; 其实此处的apppath值即为注册表中的安装路径
  ; DetailPrint "message: APPPATH: $apppath"
  ; DetailPrint "message: RESTART: $restart"

  SetDetailsPrint both
	;nsProcess::_KillProcess "${PROCESS_NAME}.exe" $R0
  ; 更新前要杀死所有相关进程
  ; 关闭翼丰信
	nsProcess::_KillProcess "${PRODUCT_CH_NAME}.exe" $R0
  Pop $R0
  ; 关闭截图工具
  nsProcess::_FindProcess "yfscreenshot.exe"
  Pop $R0
  ${If} $R0 = 0
    nsProcess::_KillProcess "yfscreenshot.exe"
  ${EndIf}
  ; 关闭文件直传工具
  nsProcess::_FindProcess "yfx_p2p.exe"
  Pop $R0
  ${If} $R0 = 0
    nsProcess::_KillProcess "yfx_p2p.exe"
  ${EndIf}
  ; 关闭会议系统
  nsProcess::_FindProcess "翼丰信会议系统.exe"
  Pop $R0
  ${If} $R0 = 0
    nsProcess::_KillProcess "翼丰信会议系统.exe"
  ${EndIf}

  nsProcess::_Unload

  SetOutPath $INSTDIR

  RMDir /r $INSTDIR

  File "hpatchz.exe"
  File "${DELTA_FILE_PATH}"

  nsExec::ExecToLog '"$INSTDIR\hpatchz.exe" -C-all "$apppath" "$INSTDIR\${DELTA_FILE_NAME}" "$apppath" -f'
  ; DetailPrint $0
  Pop $0

  ; 获取安装后路径下文件大小
  ${GetSize} "$apppath" "/S=0K" $0 $1 $2
  IntFmt $0 "0x%08X" $0

  ; WriteRegStr SHELL_CONTEXT "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${APP_GUID}" DisplayName "${PRODUCT_NAME}"
  ; WriteRegDWORD SHELL_CONTEXT "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${APP_GUID}" "EstimatedSize" "${NEW_APP_SIZE}"
  ; WriteRegStr SHELL_CONTEXT "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\${APP_GUID}" "DisplayVersion" "${NEW_APP_VERSION}"
  ; 需要此设置，否则以下写入注册表会失败
  SetRegView 64
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}" "DisplayName" "翼丰信"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}" "DisplayVersion" "${NEW_APP_VERSION}"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${APP_GUID}" "EstimatedSize" "$0"

  ; DetailPrint $apppath
  ; DetailPrint  "$apppath\${PROCESS_NAME}.exe"
  ; DetailPrint  "$apppath\翼丰信.exe"
  ; DetailPrint $restart

  ; ${If} $restart == "1"
    ; ShellExecAsUser::ShellExecAsUser "" "$apppath\${PROCESS_NAME}.exe" "--updated"
  ;    ShellExecAsUser::ShellExecAsUser "" "$apppath\翼丰信.exe" "--updated"
  ; ${Else}
      ; DetailPrint "$RESTART IS 0 switch found"
  ; ${EndIf}

  ; 安装成功后直接关闭窗口
  ; !insertmacro quitSuccess
SectionEnd

; 安装完成窗口点击确定按钮的执行函数
Function LaunchLink
    ShellExecAsUser::ShellExecAsUser "" "$apppath\${PRODUCT_CH_NAME}.exe" "--updated"
FunctionEnd

; 判断安装路径是否为空
Function CheckInstallPath
  ${If} $INSTDIR == ""
    MessageBox MB_ICONSTOP "请进行全量安装！"
    Abort
  ${EndIf}
FunctionEnd